Skip to content

fix: stop AttributesFromDSN panicking on unix-socket DSNs#625

Open
SAY-5 wants to merge 1 commit intoXSAM:mainfrom
SAY-5:fix/attributes-from-dsn-unix-socket-panic-624
Open

fix: stop AttributesFromDSN panicking on unix-socket DSNs#625
SAY-5 wants to merge 1 commit intoXSAM:mainfrom
SAY-5:fix/attributes-from-dsn-unix-socket-panic-624

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 20, 2026

Fixes #624.

AttributesFromDSN first split the DSN on the first / to drop the database/params suffix, and then unwrapped any protocol(addr) form by taking dsn[openParen+1 : len(dsn)-1]. That ordering is safe for most addresses but breaks as soon as the address itself contains a slash:

unix(/tmp/mysql.sock)/mysql?parseTime=true

The / split happens inside the parentheses, truncating dsn to unix(. The protocol unwrap then computes [openParen+1 : len(dsn)-1] = [5:4], which panics with slice bounds out of range.

This PR extracts the protocol(addr) address first by reading between the matching parens, and only falls back to the first-/ trim on bare addresses. That makes the unix-socket case round-trip to semconv.ServerAddress("/tmp/mysql.sock") and leaves every existing test case unchanged. A regression test is added for the unix-socket DSN.

@SAY-5 SAY-5 requested a review from XSAM as a code owner April 20, 2026 07:03
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.9%. Comparing base (31f7989) to head (b6709c2).

Files with missing lines Patch % Lines
helpers.go 82.3% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main    #625     +/-   ##
=======================================
- Coverage   86.4%   85.9%   -0.6%     
=======================================
  Files         16      16             
  Lines        752     752             
=======================================
- Hits         650     646      -4     
  Misses        75      75             
- Partials      27      31      +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Owner

@XSAM XSAM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SAY-5 thanks for the PR. Please update the changelog and fix the linter.

When the DSN uses the protocol(addr) form with a unix-socket path
inside the parentheses (e.g. 'unix(/tmp/mysql.sock)/dbname'),
AttributesFromDSN used to split on '/' before looking for the
wrapping parens. That produced dsn[:pathIndex] = 'unix(' and caused
an out-of-range slice panic further down the chain when
net.SplitHostPort chewed through the unbalanced parenthesis (XSAM#624).

Extract the address-extraction logic into addrFromDSN so the main
function stays under the project's cyclop max-complexity budget, and
check for the '(...)' wrapper first. If the parens are present and
balanced, use the substring between them as the address; otherwise
fall back to the previous 'trim at first /' behaviour so bare
host[:port] and host:port/db forms keep the same semantics.

Closes XSAM#624

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
@SAY-5 SAY-5 force-pushed the fix/attributes-from-dsn-unix-socket-panic-624 branch from 6f8e3fd to b6709c2 Compare April 21, 2026 06:59
@SAY-5
Copy link
Copy Markdown
Author

SAY-5 commented Apr 21, 2026

Thanks @XSAM — addressed both:

  • CHANGELOG.md: added entry under [Unreleased] / Fixed describing the unix-socket DSN panic.
  • Cyclop: extracted the DSN parsing into a new addrFromDSN helper, bringing AttributesFromDSN back under the max-complexity budget.

Verified locally on macOS, go 1.26.2:

  • gofmt -s -l: clean
  • go vet ./...: clean
  • golangci-lint run (installed latest, repo's .golangci.yaml): 0 issues
  • go test -race -count=1 ./...: pass (including the new unix-socket DSN cases)

Net diff unchanged behaviourally: bare-address, host:port, and host:port/db forms still resolve the same way; only the protocol(addr) form now extracts the addr from inside the parens first, which is what closes #624.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panics with unix domain socket DSN

2 participants